/**
 * create by young (young2f@outlook.com) on 2020/8/22
 */
'use strict';
import {
  findDynamicLabels,
  updateDLabel,
  deleteDynamicLabel,
  createDynamicLabel,
} from '../services/dynamicLabel';

export default {
  namespace: 'dynamicLabel',
  state: {},
  effects: {
    * findLabels({payload}, {call, put}) {
      const response = yield call(findDynamicLabels, payload);
      console.log("find d labels", response);
      if (response) {
        const {result} = response;
        const dLabels = result.filter(item => {
          return !item.disabled;
        });
        yield put({
          type: 'onDataLoad',
          payload: {
            dLabels,
          },
        });
      }
    },

    * createLabel({payload}, {call, put}) {
      console.log('createLabell model')
      const response = yield call(createDynamicLabel, payload);
      return response;
    },

    * updateDLabel({payload}, {call, put}) {
      const response = yield call(updateDLabel, payload);
      return response;
    },
    * deleteLabel({payload}, {call, put}) {
      const response = yield call(deleteDynamicLabel, payload);
      return response;
    },
  },

  reducers: {
    onDataLoad(state, {payload}) {
      return {...state, ...payload};
    },
  },

};
